﻿package divinity.fx.particles
{
	import flash.display.*;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.Rectangle;
	import flash.utils.*;
	
	import divinity.events.*;
	import divinity.events.WorldEvent;
	
	
	public class ParticleGenerator extends Sprite
	{
		
		//VAR INITIATE/////////////////		
		private var _fountain:Object={};		
		private var _world;
		private var _newdef:Object=null;
		private var _startTime:uint;
		private var _gen:Boolean=false;
		private var _interval:int=0;
		private var _running:Boolean=false;
		public function ParticleGenerator(wrld:EventDispatcher=null)
		{			
			if(wrld!=null)
			{
				world=wrld;				
			}
			else
			{
				_world=this;
				// TODO FP: da se startCycle gasi kada stavis novi WORLD
				startCycle();
			}
			trace("[PARTICLE GENERATOR] World: "+_world);
			reset();
		}
		/***
		 * 
		 * 
		 * 
		 * 
		 * */
		private function startCycle()
		{		
			trace("   ->"+_world);
			if(!_running)
			{
				_running=true;
				_world.addEventListener(Event.ENTER_FRAME,cycle);			
				_startTime=getTimer();
			}
		}
		public function destroy()
		{
			_world.removeEventListener(Event.ENTER_FRAME,cycle);
		}
		private function cycle(e:Event)
		{
			//trace("cycle");
			var _elapsed=getTimer()-_startTime;
			_interval+=_elapsed;
			if(_gen&&_interval>def._interval)
			{
				oneTimeGen(def._partNum);
				_interval=0;
			}
			if(_world==this)
			{
				//trace("NO WORLD");
				var we:WorldEvent=new WorldEvent(WorldEvent.CYCLE);
				we.elapsed=_elapsed;
				this.dispatchEvent(we);
				_startTime=getTimer();
			}
		}
		private function endCycle()
		{			
			_running=false;
			removeEventListener(Event.ENTER_FRAME,cycle);
		}
		/**
		 * 
		 * 
		 * 
		 * */
		public function reset()
		{	
			
			_fountain._spread=360;
			_fountain._interval=20;
			_fountain._partNum=1;
			_fountain._filters=null;
			_fountain._angle=0;
			_fountain._gravity=0;
			_fountain._wind=0;
			_fountain._img=null;
			_fountain._width=0;
			_fountain._height=0;
			_fountain._speed=[50,120];		
			_fountain._lifespan=[300,1000];		
			_fountain._rotation=[0,360];			
			_fountain._alpha=[1,0];			
			_fountain._scale=[1,0];
			_fountain._box=[0,0];
			_fountain._attachedTo=null;		
			_fountain._onDead=null;
			_fountain._onAlive=null;
			_fountain._onCreate=null;
			_fountain._followPath=false;
			_fountain._playMe=false;
			_fountain._limit=null;
			_fountain._bounce=1;
			_fountain._mass=1;
			
		}
		public function setProps(obj:Object):void
		{		
			reset();
			changeProps(obj);
			
		}
		public function changeProps(obj:Object)
		{
			for (var prop in obj)
			{							
				_fountain[prop]=obj[prop];	
			}
		}
		public function setPropsAndGen(obj:Object,num:int=10):void
		{			
			
			for (var prop in obj)
			{	
				_fountain[prop]=obj[prop];	
			}
			oneTimeGen(num);
		}
		public function oneTimeGen(ips:int):void
		{			
		
			for (var n=0; n<ips; n++)
			{
				
				createParticle();
			}
		}
		public function startGen():void
		{
			trace("start gen");
			_gen=true;
			startCycle();
		}
		public function stopGen():void
		{
			_gen=false;
			endCycle();
		}
		/*
		*
		*
		*
		*/
		private function createParticle()
		{
			
			var p:*=Particle;
			
			var part:Particle=new p(_world,_fountain);
			//part.cacheAsBitmap=true;
			//trace(this,_fountain._attachedTo)
			if(_fountain._attachedTo!=null)
			{
				
				part.x=x;
				part.y=y;
				_fountain._attachedTo.addChild(part);
			}
			else
			{
				addChild(part);
			}
		}
		/**
		 * 
		 * 
		 * 
		 * 
		 * 
		 * */
		public function get def():Object
		{
			return _fountain;	
		}
		public function set def(d:Object):void
		{
			_fountain=d;	
		}
		public function set world(w:*):void
		{
			destroy();
			_world=w;	
		}
		
	}
}